package com.ads.proplan.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ads.proplan.R;
import com.ads.proplan.control.GameOverControl;
import com.ads.proplan.db.entity.SessaoPergunta;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_gameover)
public class GameOverActivity extends Activity {
	
	private GameOverControl control;
	
	@ViewById(R.id.gameover_text_parabens)
	TextView parabensTextView;
	
	@ViewById(R.id.gameover_text_rendimento)
	TextView rendimentoTextView;
	
	@ViewById(R.id.gameover_relativelayout)
	public RelativeLayout layout;

	private int cont;

	private boolean trd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		control = GameOverControl.getInstance();
		control.initialControl(this);
		cont = 0;
		trd = true;
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		timeColor();
		setRendimento();
	}
	
	@UiThread
	public void setRendimento() {
		int contCerto = 0;
		int contErrado = 0;
		ArrayList<SessaoPergunta> arrayListSessaoPergunta = control.getNewListSessaoPergunta();
		for (SessaoPergunta sessaoPergunta : arrayListSessaoPergunta) {
			if(sessaoPergunta.getIsAcerto() == 1){
				contCerto++;
			}else{
				contErrado++;
			}
		}
		
		rendimentoTextView.setText("Correto: "+contCerto+" / Errado: "+contErrado);
	}

	@UiThread(delay = 200)
	public void timeColor() {
		cont++;
		switch (cont) {
		case 1:
			layout.setBackgroundColor(Color.parseColor("#AFEEEE"));
			break;
		case 2:
			layout.setBackgroundColor(Color.parseColor("#EEDD82"));
			break;
		case 3:
			layout.setBackgroundColor(Color.parseColor("#FFDEAD"));
			cont = 0;
			break;
		}
		if (trd) {
			timeColor();
		}
	}
	
	@Click(R.id.gameover_button_sair)
	public void onclickButomSair() {
		onBackPressed();
	}
	
	@Click(R.id.gameover_button_repetir)
	public void onclickButomRepetir() {
		SharedPreferences prefs = this.getSharedPreferences(QuestionActivity.TAG_SharedP, Context.MODE_PRIVATE);
		if (prefs.getBoolean(EndTimeActivity.TAG_DESAFIO, false)) {
			prefs.edit().putBoolean(EndTimeActivity.TAG_DESAFIO, false).commit();
		}
		restartControl();
		control.closeControl();
		CharacterActivity_.intent(this).start();
		finish();
	}
	
	@Override
	public void onBackPressed() {
		restartControl();
		control.closeControl();
		MenuActivity_.intent(this).start();
		finish();
	}

	private void restartControl() {
		trd = false;
		control.getJogadorEntity().setFase(1);
		control.restartControl();
	}
	
}
